package com.yy.yyui.turnPage
{
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;

	/**
	 *翻页组件 
	 * @author YY
	 * 
	 */
	public class TurnPage
	{
		private var _preBtn:DisplayObject;	//上一页按钮
		private var _nextBtn:DisplayObject;	//下一页按钮
		private var _currentPageTF:TextField;	//显示当前页的文本框
		private var _totalPageTF:TextField;	//显示总页数的文本框
		private var _currentPage:int = 0; //记录当前页
		private var _totalPage:int = 0;	//记录总页数
		private var _callBack:Function; //回调函数
		public static var CALL_BACK_STATE_PRE_PAGE:int = 0; //回调类型 上一页
		public static var CALL_BACK_STATE_NEXT_PAGE:int = 1;//回调类型下一页
		public function TurnPage(preBtn:DisplayObject,
								 nextBtn:DisplayObject,
								 currentPageTF:TextField,
								 totalPageTF:TextField,
								 callBack:Function=null,
								 currentPage:int=1,
								 totalPage:int=1
		)
		{
			_preBtn = preBtn;
			_nextBtn = nextBtn;
			_currentPageTF = currentPageTF;
			_totalPageTF = totalPageTF;
			_currentPage = currentPage;
			_totalPage = totalPage;
			_callBack = callBack;
			init();
		}
		
		/**
		 *释放资源 
		 * 
		 */
		private function dispose():void {
			
		}
		
		/**
		 *初始化组件 
		 * 
		 */
		private function init():void {
			//侦听事件
			addEvent();
			//设置页数显示
			showPage();
		}
		
		/**
		 *侦听事件 
		 * 
		 */
		private function addEvent():void {
			if(_preBtn) {
				_preBtn.addEventListener(MouseEvent.CLICK, preFunc);
			}
			if(_nextBtn) {
				_nextBtn.addEventListener(MouseEvent.CLICK, nextFunc);
			}
		}
		
		/**
		 *点击上一页 
		 * 
		 */
		private function preFunc(e:Event):void {
			this._currentPage--;
			if(this._currentPage < 1) {
				this._currentPage = 1;
			}
			showPage();
		}
		
		/**
		 *点击下一页 
		 * 
		 */
		private function nextFunc(e:Event):void {
			this._currentPage++;
			if(this._currentPage >= _totalPage) {
				this._currentPage = _totalPage;
			}
			showPage();
		}
		
		/**
		 *显示页数 
		 * 
		 */
		private function showPage():void {
			//显示当前页
			if(this._currentPageTF) {
				this._currentPageTF.text = String(this._currentPage);
			}
			//显示总页数
			if(this._totalPageTF) {
				this._totalPageTF.text = String(this._totalPage);
			}
			handCallBack();
		}
		
		/**
		 *设置页数 
		 * 
		 */
		public function setPage(curentPage:int, totalPage:int):void {
			_currentPage = curentPage;
			_totalPage = totalPage;
			showPage();
		}
		
		/**
		 *执行回调函数 
		 * 
		 */
		private function handCallBack():void {
			if(Boolean(_callBack)) {
				_callBack(_currentPage);
			}
		}
		
		public function get callBack():Function
		{
			return _callBack;
		}
		
		public function set callBack(value:Function):void
		{
			_callBack = value;
		}
	}
}